#import "BWGridAttributes.h"


@implementation BWGridAttributes

+ (void) initialize
{
    [super initialize];

    NSArray *keys;
    keys = [NSArray arrayWithObjects: @"showMajorLines", @"minorWeight", 
                    @"minorColor", @"minorWeightIndex", @"minorColorIndex",
                    @"majorWeightIndex", @"majorColorIndex", nil];

    [BWGridAttributes 
        setKeys: keys
        triggerChangeNotificationsForDependentKey: @"gridAttributeChange"];

    [BWGridAttributes setVersion: 1.0];
    
} // initialize



- (BOOL) gridAttributeChange
{
    return (YES);
} // gridAttributeChange


- (NSColor *) colorForKind: (BWGridAttributes_LineColor) kind
{
    NSColor *color = [NSColor blackColor];

    switch (kind) {
    case kColor_Black:
        color = [NSColor blackColor];
        break;
    case kColor_DarkGray:
        color = [NSColor darkGrayColor];
        break;
    case kColor_Gray:
        color = [NSColor grayColor];;
        break;
    case kColor_LightGray:
        color = [NSColor lightGrayColor];
        break;
    }

    return (color);

} // colorForKind


- (void) setMinorLineWeight: (BWGridAttributes_LineWeight) weight
{
    minorWeight = weight;
} // setMinorLineWeight


- (BWGridAttributes_LineWeight) minorLineWeight
{
    return (minorWeight);
} // minorLineWeight


- (float) minorLineWeightPoints
{
    return ((float)minorWeight / 100.0);
} // minorLineWeightPoints



- (void) setMajorLineWeight: (BWGridAttributes_LineWeight) weight
{
    majorWeight = weight;
} // setMajorLineWeight


- (BWGridAttributes_LineWeight) majorLineWeight
{
    return (majorWeight);
} // majorLineWeight


- (float) majorLineWeightPoints
{
    return ((float)majorWeight / 100.0);
} // majorLineWeightPoints


- (void) setMinorLineColor: (BWGridAttributes_LineColor) color
{
    minorColor = color;
} // setMinorLineColor


- (BWGridAttributes_LineColor) minorLineColor
{
    return (minorColor);
} // minorLineColor


- (NSColor *) minorLineColorColor
{
    return ([self colorForKind: minorColor]);
} // minorLineColorColor


- (void) setMajorLineColor: (BWGridAttributes_LineColor) color
{
    majorColor = color;
} // setMajorLineColor


- (BWGridAttributes_LineColor) majorLineColor
{
    return (majorColor);
} // majorLineColor

- (NSColor *) majorLineColorColor
{
    return ([self colorForKind: majorColor]);
} // majorLineColorColor


- (void) setShowMajorLines: (BOOL) yesNo
{
    showMajorLines = yesNo;
} // setShowMajorLines


- (BOOL) showMajorLines
{
    return (showMajorLines);
} // showMajorLines



- (void) setMajorLinesEvery: (int) every
{
    majorLinesEvery = every;
} // setMajorLinesEvery


- (int) majorLinesEvery
{
    return (majorLinesEvery);
} // majorLinesEvery


// --------------------------------------------------
// binding jazz

- (NSArray *) lineWeightNames
{
    return ([NSArray arrayWithObjects: 
                         @"1/4 point",
                     @"1/3 point",
                     @"1/2 point",
                     @"1 point",
                     @"2 points", nil]);

} // lineWeightNames


- (NSArray *) lineColorNames
{
    return ([NSArray arrayWithObjects:
                         @"Black",
                     @"Dark Gray",
                     @"Gray",
                     @"Light Gray", nil]);
    
} // lineColorNames


- (int) colorToIndex: (BWGridAttributes_LineColor) color
{
    int index = -1;

    // just in case there's not a 1:1 between colors and indexes
    switch (color) {
    case kColor_Black:
        index = 0;
        break;
    case kColor_DarkGray:
        index = 1;
        break;
    case kColor_Gray:
        index = 2;
        break;
    case kColor_LightGray:
        index = 3;
        break;
    }
    
    return (index);

} // colorToIndex


- (BWGridAttributes_LineColor) indexToColor: (int) index
{
    BWGridAttributes_LineColor color = kColor_Black;

    switch (index) {
    case 0:
        color = kColor_Black;
        break;
    case 1:
        color = kColor_DarkGray;
        break;
    case 2:
        color = kColor_Gray;
        break;
    case 3:
        color = kColor_LightGray;
        break;
    }

    return (color);

} // indexToColor


- (int) weightToIndex: (BWGridAttributes_LineWeight) weight
{
    int index = -1;

    switch (weight) {
    case kWeight_1_4_pt:
        index = 0;
        break;
    case kWeight_1_3_pt:
        index = 1;
        break;
    case kWeight_1_2_pt:
        index = 2;
        break;
    case kWeight_1_pt:
        index = 3;
        break;
    case kWeight_2_pt:
        index = 4;
        break;
    }

    return (index);

} // weightToIndex


- (BWGridAttributes_LineWeight) indexToWeight: (int) index
{
    BWGridAttributes_LineWeight weight;

    switch (index) {
    case 0:
        weight = kWeight_1_4_pt;
        break;
    case 1:
        weight = kWeight_1_3_pt;
        break;
    case 2:
        weight = kWeight_1_2_pt;
        break;
    case 3:
        weight = kWeight_1_pt;
        break;
    case 4:
        weight = kWeight_2_pt;
        break;
    }

    return (weight);
    
} // indexToWeight



- (int) minorWeightIndex
{
    return ([self weightToIndex: minorWeight]);
} // minorWeightIndex


- (void) setMinorWeightIndex: (int) index
{
    [self setMinorLineWeight: [self indexToWeight: index]];
} // setMinorWeightIndex


- (int) majorWeightIndex
{
    return ([self weightToIndex: majorWeight]);
} // majorWeightIndex


- (void) setMajorWeightIndex: (int) index
{
    [self setMajorLineWeight: [self indexToWeight: index]];
} // setMajorWeightIndex


- (int) minorColorIndex
{
    return ([self colorToIndex: minorColor]);
} // minorColorIndex


- (void) setMinorColorIndex: (int) index
{
    [self setMinorLineColor: [self indexToColor: index]];
} // setMinorColorIndex


- (int) majorColorIndex
{
    return ([self colorToIndex: majorColor]);
} // majorColorIndex


- (void) setMajorColorIndex: (int) index
{
    [self setMajorLineColor: [self indexToColor: index]];
} // setMajorColorIndex


- (void) encodeWithCoder: (NSCoder *) coder
{
    [coder encodeValueOfObjCType: @encode(BWGridAttributes_LineWeight)
           at: &minorWeight];
    [coder encodeValueOfObjCType: @encode(BWGridAttributes_LineColor)
           at: &minorColor];

    [coder encodeValueOfObjCType: @encode(BOOL)
           at: &showMajorLines];
    [coder encodeValueOfObjCType: @encode(int)
           at: &majorLinesEvery];

    [coder encodeValueOfObjCType: @encode(BWGridAttributes_LineWeight)
           at: &majorWeight];
    [coder encodeValueOfObjCType: @encode(BWGridAttributes_LineColor)
           at: &majorColor];
    
} // encodeWithCoder


- (id) initWithCoder: (NSCoder *) coder
{
    if (self = [super init]) {
        [coder decodeValueOfObjCType: @encode(BWGridAttributes_LineWeight)
               at: &minorWeight];
        [coder decodeValueOfObjCType: @encode(BWGridAttributes_LineColor)
               at: &minorColor];
        
        [coder decodeValueOfObjCType: @encode(BOOL)
               at: &showMajorLines];
        [coder decodeValueOfObjCType: @encode(int)
               at: &majorLinesEvery];
        
        [coder decodeValueOfObjCType: @encode(BWGridAttributes_LineWeight)
               at: &majorWeight];
        [coder decodeValueOfObjCType: @encode(BWGridAttributes_LineColor)
               at: &majorColor];
    }
    
    return (self);

} // coder


@end // BWGridAttributes

